package com.huiminpay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.merchant.api.IMerchantService;
import com.huiminpay.merchant.covert.MerchantCovert;
import com.huiminpay.merchant.dto.MerchantDTO;
import com.huiminpay.merchant.entity.Merchant;
import com.huiminpay.merchant.entity.Staff;
import com.huiminpay.merchant.entity.Store;
import com.huiminpay.merchant.entity.StoreStaff;
import com.huiminpay.merchant.mapper.MerchantMapper;
import com.huiminpay.merchant.mapper.StaffMapper;
import com.huiminpay.merchant.mapper.StoreMapper;
import com.huiminpay.merchant.mapper.StoreStaffMapper;
import com.huiminpay.user.api.TenantService;
import com.huiminpay.user.api.dto.tenant.CreateTenantRequestDTO;
import com.huiminpay.user.api.dto.tenant.TenantDTO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@org.apache.dubbo.config.annotation.Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,readOnly = false)
public class IMerchantServiceImpl implements IMerchantService {

    @Autowired
    MerchantMapper merchantMapper;

    @Reference
    TenantService tenantService;

    @Autowired
    StoreMapper storeMapper;

    @Autowired
    StaffMapper staffMapper;

    @Autowired
    StoreStaffMapper storeStaffMapper;

    @Override
    @Transactional(readOnly = true)
    public MerchantDTO queryMerchantById(Long merchantId) {
        Merchant merchant = merchantMapper.selectById(merchantId);
        MerchantDTO merchantDTO = MerchantCovert.INSTANCE.entity2Dto(merchant);
        return merchantDTO;
    }

    @Override
    public MerchantDTO insertMerchant(MerchantDTO merchantDTO) throws BusinessException {

        //非空校验
        if (merchantDTO.getMobile() == null) {
            throw new BusinessException(CommonErrorCode.E_200230);
        }
        if (merchantDTO.getUsername() == null) {
            throw new BusinessException(CommonErrorCode.E_200231);
        }
        if (merchantDTO.getPassword() == null) {
            throw new BusinessException(CommonErrorCode.E_200232);
        }

        Merchant merchant = MerchantCovert.INSTANCE.dto2Entity(merchantDTO);

        //验证电话号码是否唯一
        QueryWrapper<Merchant> qw = new QueryWrapper<>();
        qw.lambda().eq(Merchant::getMobile,merchant.getMobile());
        Integer result = merchantMapper.selectCount(qw);
        if (result > 0){
            throw new BusinessException(CommonErrorCode.E_200203);
        }


        //1.对接saas平台，新增租户，并初始化管理员
        CreateTenantRequestDTO createTenantRequestDTO = new CreateTenantRequestDTO();
        createTenantRequestDTO.setName(merchantDTO.getUsername());
        createTenantRequestDTO.setMobile(merchantDTO.getMobile());
        createTenantRequestDTO.setUsername(merchantDTO.getUsername());
        createTenantRequestDTO.setPassword(merchantDTO.getPassword());
        createTenantRequestDTO.setTenantTypeCode("huimin-merchant");
        createTenantRequestDTO.setBundleCode("huimin-merchant");

        TenantDTO tenantAndAccount = tenantService.createTenantAndAccount(createTenantRequestDTO);


        //2.将新增商户id返回
        //设置审核状态0‐未申请,1‐已申请待审核,2‐审核通过,3‐审核拒绝
        merchant.setAuditStatus("0");
        //TODO tenantId
        merchant.setTenantId(tenantAndAccount.getId());
        merchantMapper.insert(merchant);

        merchantDTO.setId(merchant.getId());


        //3.初始化商店信息

        Store store = new Store();
        store.setStoreStatus(true);
        store.setStoreName("根门店");
        store.setStoreNumber(99999l);
        store.setParentId(0l);
        store.setMerchantId(merchant.getId());
        storeMapper.insert(store);
        //4.初始化员工信息
        Staff staff = new Staff();
        staff.setStaffStatus(true);
        staff.setMobile(merchantDTO.getMobile());
        staff.setMerchantId(merchant.getId());
        staff.setStoreId(store.getId());
        staff.setUsername(merchantDTO.getUsername());
        staffMapper.insert(staff);
        //5.为门店初始化管理员
        StoreStaff storeStaff = new StoreStaff();
        storeStaff.setStaffId(staff.getId());
        storeStaff.setStoreId(store.getId());
        storeStaffMapper.insert(storeStaff);

        return merchantDTO;
    }

    @Override
    public MerchantDTO updateMerchant(MerchantDTO merchantDTO) throws BusinessException {

        //非空校验
        if (merchantDTO.getId() == null){
            throw new BusinessException(CommonErrorCode.E_100105);
        }
        Merchant merchant = MerchantCovert.INSTANCE.dto2Entity(merchantDTO);
        //不能更新电话，用户名，租户ID
        merchant.setMobile(null);
        merchant.setUsername(null);
        merchant.setTenantId(null);
        //更新状态已申请待审核  1
        merchant.setAuditStatus("1");
        merchantMapper.updateById(merchant);

        return MerchantCovert.INSTANCE.entity2Dto(merchant);
    }

    @Override
    public MerchantDTO queryMerchantByTenantId(Long tenantId) {
        QueryWrapper<Merchant> qw = new QueryWrapper<>();
        qw.lambda().eq(Merchant::getTenantId,tenantId);

        Merchant merchant = merchantMapper.selectOne(qw);

        return MerchantCovert.INSTANCE.entity2Dto(merchant);
    }
}
